{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DesignerTheme;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, PngImageList, ThemeDescriptor;

type
  TAHMThemeMode = (tmAdding, tmEditing, tmSaving);

  TfrmThemeProperties = class(TForm)
    lbCaption: TLabel;
    lbAction: TLabel;
    pbHeader: TPaintBox;
    btOkay: TButton;
    btCancel: TButton;
    Bevel1: TBevel;
    Label1: TLabel;
    edName: TEdit;
    Label2: TLabel;
    edDescription: TEdit;
    Label3: TLabel;
    edVersion: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    edMinVersion: TEdit;
    edAuthor: TEdit;
    edHomepage: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    edLicense: TEdit;
    Label11: TLabel;
    Label12: TLabel;
    edWidth: TEdit;
    edHeight: TEdit;
    mmDetails: TMemo;
    procedure edNameChange(Sender: TObject);
    procedure pbHeaderPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btOkayClick(Sender: TObject);
  private
    { Private declarations }
    FHeaderImages: TPngImageList;
    FOldName: String;
    FTheme: TAHMThemeDescriptor;
    FMode: TAHMThemeMode;
    procedure LoadImages;
  protected
    { Protected declarations }
    procedure ConfigureForm;
    procedure PopulateForm;
    procedure ValidateForm;
    procedure PopulateProperties;
  public
    { Public declarations }
    property Theme: TAHMThemeDescriptor read FTheme write FTheme;
    property Mode: TAHMThemeMode read FMode write FMode;
    class function NewTheme(Theme: TAHMThemeDescriptor): Boolean;
    class function EditTheme(Theme: TAHMThemeDescriptor): Boolean;
    class function SaveTheme(Theme: TAHMThemeDescriptor): Boolean;
  end;


implementation

{$R *.dfm}

uses ApplicationManager;

var
  FormInstance: TfrmThemeProperties;

//-----------------------------------------------------------------------------
//----------------------------- Class Functions -------------------------------
//-----------------------------------------------------------------------------

class function TfrmThemeProperties.NewTheme(Theme: TAHMThemeDescriptor): Boolean;
begin
  // Set default result - failed
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmThemeProperties.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  FormInstance.Theme := Theme;
  FormInstance.Mode := tmAdding;

  if FormInstance.ShowModal = mrOK then
  begin
    FormInstance.PopulateProperties;
    Result := True;
  end;
end;

class function TfrmThemeProperties.EditTheme(Theme: TAHMThemeDescriptor): Boolean;
begin
  // Set default result - failed
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmThemeProperties.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  FormInstance.Theme := Theme;
  FormInstance.Mode := tmEditing;

  if FormInstance.ShowModal = mrOK then
  begin
    FormInstance.PopulateProperties;
    Result := True;
  end;
end;

class function TfrmThemeProperties.SaveTheme(Theme: TAHMThemeDescriptor): Boolean;
begin
  // Set default result - failed
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmThemeProperties.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  FormInstance.Theme := Theme;
  FormInstance.Mode := tmSaving;

  if FormInstance.ShowModal = mrOK then
  begin
    FormInstance.PopulateProperties;
    Result := True;
  end;
end;


//-----------------------------------------------------------------------------
//----------------------------- Form Events -----------------------------------
//-----------------------------------------------------------------------------

procedure TfrmThemeProperties.FormCreate(Sender: TObject);
begin
  // Create and populate PNG image list
  FHeaderImages := TPngImageList.Create(Self);
  LoadImages;
end;

procedure TfrmThemeProperties.FormShow(Sender: TObject);
begin
  // Store current theme name
  FOldName := FTheme.Name;

  ConfigureForm;
end;

//-----------------------------------------------------------------------------
//----------------------------- Control Events --------------------------------
//-----------------------------------------------------------------------------

procedure TfrmThemeProperties.edNameChange(Sender: TObject);
begin
  ValidateForm;
end;

procedure TfrmThemeProperties.btOkayClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TfrmThemeProperties.pbHeaderPaint(Sender: TObject);
begin
  with pbHeader do
  begin
    // Draw background
    Canvas.Brush.Color := clWhite;
    Canvas.FillRect(GetClientRect);
    // Draw appropriate header image
    FHeaderImages.Draw(Canvas, ClientWidth - FHeaderImages.Width, 0, 0);
  end;
end;

//-----------------------------------------------------------------------------
//-------------------------- User Interface Update ----------------------------
//-----------------------------------------------------------------------------

procedure TfrmThemeProperties.ConfigureForm;
begin
  case FMode of
    tmAdding:
    begin
      Caption := 'New Theme';
      lbCaption.Caption := 'Add a new theme';
      lbAction.Caption := 'You may enter details for the new theme below';
    end;
    tmEditing:
    begin
      Caption := 'Edit Theme';
      lbCaption.Caption := 'Edit theme details';
      lbAction.Caption := 'You may modify the theme details below';
    end;
    tmSaving:
    begin
      Caption := 'Save Theme As';
      lbCaption.Caption := 'Enter theme name';
      lbAction.Caption := 'You may enter a new name to save the theme as below';
    end;
  end;

  // Populate form controls
  PopulateForm;
  ValidateForm;

  // Start focus in the name/description control
  edName.Enabled := FMode in [tmAdding, tmSaving];
  if edName.Enabled then edName.SetFocus
                    else edDescription.SetFocus;
end;

procedure TfrmThemeProperties.PopulateForm;
begin
  with FTheme do
  begin
    edName.Text := Name;
    edDescription.Text := Description;
    edVersion.Text := Version;
    edMinVersion.Text := MinVersion;
    edAuthor.Text := Author;
    edHomepage.Text := Homepage;
    edLicense.Text := License;
    edWidth.Text := IntToStr(DesignedWidth);
    edHeight.Text := IntToStr(DesignedHeight);
    mmDetails.Text := Details;
  end;
end;

procedure TfrmThemeProperties.ValidateForm;
var
  Valid: Boolean;
begin
  // Validate appropriate theme properties
  with ApplicationManagerInstance.ThemeManager do
    case FMode of
      tmAdding:
        // Ensure the entered theme name is unique and valid
        Valid := ValidateThemeName(edName.Text, True);
      tmSaving:
        // Keep then same name, or ensure the new theme name is unique and valid
        Valid := (UpperCase(Trim(edName.Text)) = UpperCase(FOldName)) or
                 ValidateThemeName(edName.Text, True);
      else
        Valid := True;
    end;

  btOkay.Enabled := Valid;
end;

procedure TfrmThemeProperties.PopulateProperties;
begin
  with FTheme do
  begin
    if FMode <> tmEditing then Name := Trim(edName.Text);
    Description := edDescription.Text;
    Version := edVersion.Text;
    MinVersion := edMinVersion.Text;
    Author := edAuthor.Text;
    Homepage := edHomepage.Text;
    License := edLicense.Text;
    DesignedWidth := StrToIntDef(edWidth.Text, DesignedWidth);
    DesignedHeight := StrToIntDef(edHeight.Text, DesignedHeight);
    Details := mmDetails.Text;
  end;
end;

procedure TfrmThemeProperties.LoadImages;
begin
  with FHeaderImages do
  begin
    Clear;
    Width := 150; Height := 57;

    AddResourcePng('HD_IMG_GENERIC'); //0
  end;
end;


end.
